import axios from 'axios';
import {useUserInfoStore} from "../store/modules/userInfo.js";

// 配置新建一个 axios 实例
const service = axios.create({
	baseURL: "http://gmall-h5-api.atguigu.cn/api",
	timeout: 50000,
});

// 添加请求拦截器
service.interceptors.request.use(
	(config) => {
    // 如果有token,应该携带token
		const storeUserInfo = useUserInfoStore(); // 拿到store
		const token = storeUserInfo.token;
		if (token) {
			config.headers.token = token;
		}
		return config;
	}
);

// 添加响应拦截器
service.interceptors.response.use(
	(response) => {

		const res = response.data;
		return res.data;

	}
);
export default service;
